package com.innovations.inn.controller;

import com.innovations.common.response.DataResponseMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "报表模块")
@RestController
@Validated
@RequestMapping(value = "/WebAPI/Report")
public class ReportController {

    @Value("${report.url}")
    private String reportUrl;

    @ApiOperation(value = "获取Ureport地址")
    @GetMapping(value = "/getUreportUrl", produces = "application/json;charset=utf-8")
    public DataResponseMessage getUreportUrl() {
        DataResponseMessage dataResponseMessage = new DataResponseMessage();
        try {
            dataResponseMessage.setData(reportUrl);
        } catch (Exception e) {
            throw new RuntimeException("获取Ureport地址异常，异常原因：" + e.getMessage());
        }
        return dataResponseMessage;
    }

    @ApiOperation(value = "【采购】数据报表")
    @GetMapping(value = "/purchaseData", produces = "application/json;charset=utf-8")
    public DataResponseMessage<String> purchaseData(@RequestParam(required = false) Long batchId) {
        String url = reportUrl.concat("?_u=file:purchaseData.ureport.xml");
        if (batchId != null) {
            url = url.concat("&batchId=").concat(batchId.toString());
        }
        DataResponseMessage<String> dataResponseMessage = new DataResponseMessage<>();
        dataResponseMessage.setData(url);
        return dataResponseMessage;
    }

}
